package com.yq.activemq.message_filter;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

public class Receiver {

    public static final String BROKER_URL = "tcp://localhost:61616";

    public static void main(String[] args) {
        Receiver receiver = new Receiver();
        receiver.receiveMessage();
    }

    private void receiveMessage() {

        Connection connection = null;
        Session session = null;
        MessageConsumer consumer = null;

        try {
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
            connection = connectionFactory.createConnection();
            session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);

            Queue destination = session.createQueue("my-filter-queue");

            String selector = "id >=10 and id<=15";
            consumer = session.createConsumer(destination, selector);

            connection.start();

            consumer.setMessageListener(message -> {
                try {
                    String text = ((TextMessage) message).getText();
                    System.out.println("接收到的消息内容是：" + text);
                } catch (JMSException e) {
                    e.printStackTrace();
                }
            });
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }
}
